﻿// =================================================================== 
// 项目说明
//====================================================================
// wup。@Copy Right 2014
// 文件： Applications.cs
// 项目名称：买车网
// 创建时间：2015/6/24
// 负责人：wup
// ===================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Weixin.Entity.ChinaTax;
using Weixin.Utils;



namespace Weixin.DAL.ChinaTax
{
    /// <summary>
    /// 数据层实例化接口类 dbo.Applications.
    /// </summary>
    public partial class ApplicationsDataAccessLayer 
    {
		#region 构造函数


		/// <summary>
		/// 
		/// </summary>
		public ApplicationsDataAccessLayer()
		{
		}
		#endregion

        #region -----------实例化接口函数-----------
		
		#region 添加更新删除
		/// <summary>
		/// 向数据库中插入一条新记录。
		/// </summary>
		/// <param name="_ApplicationsModel">Applications实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Insert(ApplicationsEntity _ApplicationsModel)
		{
			string sqlStr="insert into Applications([CourseName],[CourseId],[Name],[Sex],[Mobile],[Email],[Company],[CreateTime],[UserName],[Referee],[UserId],[Status]) values(@CourseName,@CourseId,@Name,@Sex,@Mobile,@Email,@Company,@CreateTime,@UserName,@Referee,@UserId,@Status) select @@identity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@CourseName",SqlDbType.VarChar),
			new SqlParameter("@CourseId",SqlDbType.Int),
			new SqlParameter("@Name",SqlDbType.VarChar),
			new SqlParameter("@Sex",SqlDbType.Int),
			new SqlParameter("@Mobile",SqlDbType.VarChar),
			new SqlParameter("@Email",SqlDbType.VarChar),
			new SqlParameter("@Company",SqlDbType.VarChar),
			new SqlParameter("@CreateTime",SqlDbType.DateTime),
			new SqlParameter("@UserName",SqlDbType.VarChar),
			new SqlParameter("@Referee",SqlDbType.VarChar),
			new SqlParameter("@UserId",SqlDbType.Int),
			new SqlParameter("@Status",SqlDbType.SmallInt)
			};			
			_param[0].Value=_ApplicationsModel.CourseName;
			_param[1].Value=_ApplicationsModel.CourseId;
			_param[2].Value=_ApplicationsModel.Name;
			_param[3].Value=_ApplicationsModel.Sex;
			_param[4].Value=_ApplicationsModel.Mobile;
			_param[5].Value=_ApplicationsModel.Email;
			_param[6].Value=_ApplicationsModel.Company;
			_param[7].Value=_ApplicationsModel.CreateTime;
			_param[8].Value=_ApplicationsModel.UserName;
			_param[9].Value=_ApplicationsModel.Referee;
			_param[10].Value=_ApplicationsModel.UserId;
			_param[11].Value=_ApplicationsModel.Status;
			res = Convert.ToInt32(SqlHelper.ExecuteScalar(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param));
			return res;
		}
		/// <summary>
		/// 向数据库中插入一条新记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_ApplicationsModel">Applications实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Insert(SqlTransaction sp,ApplicationsEntity _ApplicationsModel)
		{
			string sqlStr="insert into Applications([CourseName],[CourseId],[Name],[Sex],[Mobile],[Email],[Company],[CreateTime],[UserName],[Referee],[UserId],[Status]) values(@CourseName,@CourseId,@Name,@Sex,@Mobile,@Email,@Company,@CreateTime,@UserName,@Referee,@UserId,@Status) select @@identity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@CourseName",SqlDbType.VarChar),
			new SqlParameter("@CourseId",SqlDbType.Int),
			new SqlParameter("@Name",SqlDbType.VarChar),
			new SqlParameter("@Sex",SqlDbType.Int),
			new SqlParameter("@Mobile",SqlDbType.VarChar),
			new SqlParameter("@Email",SqlDbType.VarChar),
			new SqlParameter("@Company",SqlDbType.VarChar),
			new SqlParameter("@CreateTime",SqlDbType.DateTime),
			new SqlParameter("@UserName",SqlDbType.VarChar),
			new SqlParameter("@Referee",SqlDbType.VarChar),
			new SqlParameter("@UserId",SqlDbType.Int),
			new SqlParameter("@Status",SqlDbType.SmallInt)
			};			
			_param[0].Value=_ApplicationsModel.CourseName;
			_param[1].Value=_ApplicationsModel.CourseId;
			_param[2].Value=_ApplicationsModel.Name;
			_param[3].Value=_ApplicationsModel.Sex;
			_param[4].Value=_ApplicationsModel.Mobile;
			_param[5].Value=_ApplicationsModel.Email;
			_param[6].Value=_ApplicationsModel.Company;
			_param[7].Value=_ApplicationsModel.CreateTime;
			_param[8].Value=_ApplicationsModel.UserName;
			_param[9].Value=_ApplicationsModel.Referee;
			_param[10].Value=_ApplicationsModel.UserId;
			_param[11].Value=_ApplicationsModel.Status;
			res = Convert.ToInt32(SqlHelper.ExecuteScalar(sp,CommandType.Text,sqlStr,_param));
			return res;
		}


		/// <summary>
		/// 向数据表Applications更新一条记录。
		/// </summary>
		/// <param name="_ApplicationsModel">_ApplicationsModel</param>
		/// <returns>影响的行数</returns>
		public int Update(ApplicationsEntity _ApplicationsModel)
		{
            string sqlStr="update Applications set [CourseName]=@CourseName,[CourseId]=@CourseId,[Name]=@Name,[Sex]=@Sex,[Mobile]=@Mobile,[Email]=@Email,[Company]=@Company,[CreateTime]=@CreateTime,[UserName]=@UserName,[Referee]=@Referee,[UserId]=@UserId,[Status]=@Status where AppId=@AppId";
			SqlParameter[] _param={				
				new SqlParameter("@AppId",SqlDbType.Int),
				new SqlParameter("@CourseName",SqlDbType.VarChar),
				new SqlParameter("@CourseId",SqlDbType.Int),
				new SqlParameter("@Name",SqlDbType.VarChar),
				new SqlParameter("@Sex",SqlDbType.Int),
				new SqlParameter("@Mobile",SqlDbType.VarChar),
				new SqlParameter("@Email",SqlDbType.VarChar),
				new SqlParameter("@Company",SqlDbType.VarChar),
				new SqlParameter("@CreateTime",SqlDbType.DateTime),
				new SqlParameter("@UserName",SqlDbType.VarChar),
				new SqlParameter("@Referee",SqlDbType.VarChar),
				new SqlParameter("@UserId",SqlDbType.Int),
				new SqlParameter("@Status",SqlDbType.SmallInt)
				};				
				_param[0].Value=_ApplicationsModel.AppId;
				_param[1].Value=_ApplicationsModel.CourseName;
				_param[2].Value=_ApplicationsModel.CourseId;
				_param[3].Value=_ApplicationsModel.Name;
				_param[4].Value=_ApplicationsModel.Sex;
				_param[5].Value=_ApplicationsModel.Mobile;
				_param[6].Value=_ApplicationsModel.Email;
				_param[7].Value=_ApplicationsModel.Company;
				_param[8].Value=_ApplicationsModel.CreateTime;
				_param[9].Value=_ApplicationsModel.UserName;
				_param[10].Value=_ApplicationsModel.Referee;
				_param[11].Value=_ApplicationsModel.UserId;
				_param[12].Value=_ApplicationsModel.Status;
			return SqlHelper.ExecuteNonQuery(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param);
		}
		/// <summary>
		/// 向数据表Applications更新一条记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_ApplicationsModel">_ApplicationsModel</param>
		/// <returns>影响的行数</returns>
		public int Update(SqlTransaction sp,ApplicationsEntity _ApplicationsModel)
		{
            string sqlStr="update Applications set [CourseName]=@CourseName,[CourseId]=@CourseId,[Name]=@Name,[Sex]=@Sex,[Mobile]=@Mobile,[Email]=@Email,[Company]=@Company,[CreateTime]=@CreateTime,[UserName]=@UserName,[Referee]=@Referee,[UserId]=@UserId,[Status]=@Status where AppId=@AppId";
			SqlParameter[] _param={				
				new SqlParameter("@AppId",SqlDbType.Int),
				new SqlParameter("@CourseName",SqlDbType.VarChar),
				new SqlParameter("@CourseId",SqlDbType.Int),
				new SqlParameter("@Name",SqlDbType.VarChar),
				new SqlParameter("@Sex",SqlDbType.Int),
				new SqlParameter("@Mobile",SqlDbType.VarChar),
				new SqlParameter("@Email",SqlDbType.VarChar),
				new SqlParameter("@Company",SqlDbType.VarChar),
				new SqlParameter("@CreateTime",SqlDbType.DateTime),
				new SqlParameter("@UserName",SqlDbType.VarChar),
				new SqlParameter("@Referee",SqlDbType.VarChar),
				new SqlParameter("@UserId",SqlDbType.Int),
				new SqlParameter("@Status",SqlDbType.SmallInt)
				};				
				_param[0].Value=_ApplicationsModel.AppId;
				_param[1].Value=_ApplicationsModel.CourseName;
				_param[2].Value=_ApplicationsModel.CourseId;
				_param[3].Value=_ApplicationsModel.Name;
				_param[4].Value=_ApplicationsModel.Sex;
				_param[5].Value=_ApplicationsModel.Mobile;
				_param[6].Value=_ApplicationsModel.Email;
				_param[7].Value=_ApplicationsModel.Company;
				_param[8].Value=_ApplicationsModel.CreateTime;
				_param[9].Value=_ApplicationsModel.UserName;
				_param[10].Value=_ApplicationsModel.Referee;
				_param[11].Value=_ApplicationsModel.UserId;
				_param[12].Value=_ApplicationsModel.Status;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.Text,sqlStr,_param);
		}		
		
		
		/// <summary>
		/// 删除数据表Applications中的一条记录
		/// </summary>
	    /// <param name="AppId">appId</param>
		/// <returns>影响的行数</returns>
		public int Delete(int AppId)
		{
			string sqlStr="delete from Applications where [AppId]=@AppId";
			SqlParameter[] _param={			
			new SqlParameter("@AppId",SqlDbType.Int),
			
			};			
			_param[0].Value=AppId;
			return SqlHelper.ExecuteNonQuery(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表Applications中的一条记录,带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
	    /// <param name="AppId">appId</param>
		/// <returns>影响的行数</returns>
		public int Delete(SqlTransaction sp,int AppId)
		{
			string sqlStr="delete from Applications where [AppId]=@AppId";
			SqlParameter[] _param={			
			new SqlParameter("@AppId",SqlDbType.Int),
			
			};			
			_param[0].Value=AppId;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.Text,sqlStr,_param);
		}
		#endregion
		
		#region 数据实体
		/// <summary>
		/// 得到  applications 数据实体
		/// </summary>
		/// <param name="row">row</param>
		/// <returns>applications 数据实体</returns>
		public ApplicationsEntity Populate_ApplicationsEntity_FromDr(DataRow row)
		{
			ApplicationsEntity Obj = new ApplicationsEntity();
			if(row!=null)
			{
				Obj.AppId = (( row["AppId"])==DBNull.Value)?0:Convert.ToInt32( row["AppId"]);
				Obj.CourseName =  row["CourseName"].ToString();
				Obj.CourseId = (( row["CourseId"])==DBNull.Value)?0:Convert.ToInt32( row["CourseId"]);
				Obj.Name =  row["Name"].ToString();
				Obj.Sex = (( row["Sex"])==DBNull.Value)?0:Convert.ToInt32( row["Sex"]);
				Obj.Mobile =  row["Mobile"].ToString();
				Obj.Email =  row["Email"].ToString();
				Obj.Company =  row["Company"].ToString();
				Obj.CreateTime = (( row["CreateTime"])==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime( row["CreateTime"]);
				Obj.UserName =  row["UserName"].ToString();
				Obj.Referee =  row["Referee"].ToString();
				Obj.UserId = (( row["UserId"])==DBNull.Value)?0:Convert.ToInt32( row["UserId"]);
				Obj.Status = (( row["Status"])==DBNull.Value)? (short)0 : (short)  row["Status"];
			}
			else
			{
				return null;
			}
			return Obj;
		}
		
        /// <summary>
		/// 得到  applications 数据实体
		/// </summary>
		/// <param name="dr">dr</param>
		/// <returns>applications 数据实体</returns>
		public ApplicationsEntity Populate_ApplicationsEntity_FromDr(IDataReader dr)
		{
			ApplicationsEntity Obj = new ApplicationsEntity();
			
				Obj.AppId = (( dr["AppId"])==DBNull.Value)?0:Convert.ToInt32( dr["AppId"]);
				Obj.CourseName =  dr["CourseName"].ToString();
				Obj.CourseId = (( dr["CourseId"])==DBNull.Value)?0:Convert.ToInt32( dr["CourseId"]);
				Obj.Name =  dr["Name"].ToString();
				Obj.Sex = (( dr["Sex"])==DBNull.Value)?0:Convert.ToInt32( dr["Sex"]);
				Obj.Mobile =  dr["Mobile"].ToString();
				Obj.Email =  dr["Email"].ToString();
				Obj.Company =  dr["Company"].ToString();
				Obj.CreateTime = (( dr["CreateTime"])==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime( dr["CreateTime"]);
				Obj.UserName =  dr["UserName"].ToString();
				Obj.Referee =  dr["Referee"].ToString();
				Obj.UserId = (( dr["UserId"])==DBNull.Value)?0:Convert.ToInt32( dr["UserId"]);
				Obj.Status = (( dr["Status"])==DBNull.Value)? (short)0 : (short)  dr["Status"];
			
			return Obj;
		}
		#endregion
		
		/// <summary>
		/// 根据ID,返回一个Applications对象
		/// </summary>
		/// <param name="appId">appId</param>
		/// <returns>Applications对象</returns>
		public ApplicationsEntity Get_ApplicationsEntity (int appId)
		{
			ApplicationsEntity _obj=null;
			SqlParameter[] _param={
			new SqlParameter("@AppId",SqlDbType.Int)
			};
			_param[0].Value=appId;
			string sqlStr="select * from Applications with(nolock) where AppId=@AppId";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param))
			{
				while(dr.Read())
				{
            		_obj=Populate_ApplicationsEntity_FromDr(dr);
				}
			}
			return _obj;
		}
		/// <summary>
		/// 得到数据表Applications所有记录
		/// </summary>
		/// <returns>数据集</returns>
		public IList< ApplicationsEntity> Get_ApplicationsAll()
		{
			IList< ApplicationsEntity> Obj=new List< ApplicationsEntity>();
			string sqlStr="select * from Applications";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(WebConfig.ChinaTax,CommandType.Text,sqlStr))
			{
				while(dr.Read())
				{
					Obj.Add(Populate_ApplicationsEntity_FromDr(dr));
				}
			}
			return Obj;
		}
		/// <summary>
        /// 检测是否存在根据主键
        /// </summary>
        /// <param name="appId">appId</param>
        /// <returns>是/否</returns>
		public bool IsExistApplications(int appId)
		{
			SqlParameter[] _param={
                                      new SqlParameter("@appId",SqlDbType.Int)
                                  };
            _param[0].Value=appId;
            string sqlStr="select Count(*) from Applications where AppId=@appId";
            int a=Convert.ToInt32(SqlHelper.ExecuteScalar(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param));
            if(a>0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}

        #endregion
		
		#region----------自定义实例化接口函数----------
		#endregion
    }
}
